
package command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import command.Impl.LookCommoditiesInfo;
import command.Impl.LookMerchantInfo;
import command.Impl.LookOrderInfo;
import command.Impl.ShellCommodity;
import command.Impl.UpdateCommodityPrice;
import command.Impl.UpdateCommodityStatus;
import command.Impl.UpdateMerchantAccoutInfo;
import command.Impl.UpdateMerchantInfo;
import command.Impl.UpdateOrderStatus;
import pojo.Merchant;

public class MerchantCommandFactory extends SystemCommandFactory{
	private static Map<CommandCode, Class<? extends Command>> mapMerchantCommand = new HashMap<>();

	static {
		mapMerchantCommand.put(CommandCode.LOOKMERCHANTINFO, LookMerchantInfo.class);
		mapMerchantCommand.put(CommandCode.LOOKORDERINFO, LookOrderInfo.class);
		mapMerchantCommand.put(CommandCode.LOOKCOMMODITIESINFO, LookCommoditiesInfo.class);
		mapMerchantCommand.put(CommandCode.UPDATECOMMODITYPRICE, UpdateCommodityPrice.class);
		mapMerchantCommand.put(CommandCode.UPDATECOMMODITYSTATUS, UpdateCommodityStatus.class);
		mapMerchantCommand.put(CommandCode.UPDATEMERCHANTACCOUNTINFO, UpdateMerchantAccoutInfo.class);
		mapMerchantCommand.put(CommandCode.UPDATEMERCHANTINFO, UpdateMerchantInfo.class);
		mapMerchantCommand.put(CommandCode.UPDATEORDERSTATUS, UpdateOrderStatus.class);
		mapMerchantCommand.put(CommandCode.SHELLCOMMODITY, ShellCommodity.class);
	}

	protected Merchant merchant;

	public MerchantCommandFactory(Merchant merchant) {
		this.merchant = merchant;
	}
	
	public Command buildCommand(CommandCode code) {
		Command command = null;
		if (mapMerchantCommand.get(code) != null) {
			try {
				Constructor<? extends Command> cons = mapMerchantCommand.get(code).getConstructor(Merchant.class);
				command = cons.newInstance(merchant);
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		} else {
			command = super.buildCommand(code);
		}
		return command;
	}
}
